
let self
var app = getApp();
const { FILTERS } = require('../../config/filters.js');
const { mayjorList, addressList } = require('../../config/api/index.js');
Page({
	data: {
		filters: [],
		currentFilter: [],  // 当前选择的筛选
		channelName: '',  // 通过哪种方式进入筛选列表页 地区筛选或专业筛选
		curPage: 1,
		perPage: 10,
		addressPage: 1,
		mayjorPage: 1,
		address: [],
		mayjor: [],
	},
  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		self = this;
		console.log(options)
		wx.showLoading({
			title: '加载中...',
			mask: true
		})
		wx.setNavigationBarTitle({
			title: options.title || '在职研'
		})

		self.setData({
			title: options.title || '在职研',
			channelName: options.filters
		})
		self.data.channelName == 'majorfilter' ? self.getMayjorList() : self.getAddressList()
	},
	getAddressList(param) {  // 获取地区列表
		const { perPage, addressPage, address } = self.data;
		return addressList({
			data: { per_page: 100, ...param },
			success(res) {
				
				let totalPages = res.data.total_pages
				if (addressPage <= totalPages) {
					self.setData({
						addressPage: addressPage + 1,
						address: [...address, ...res.data.items]
					})
					setTimeout(function () {
						self.getAddressList({ page: addressPage + 1 })
					}, 0)
				} else {
					let data = self.data.address;
					let address = [];
					for (let i = 0; i < data.length; i++) {
						if (data[i].level === '2') {
							address.push({
								typeid: data[i].id,
								val: data[i].name,
								lists: []
							})
						}
					}
					for (let j = 0; j < data.length; j++) {
						let currentItem = address.find(item => {
							return data[j].pid === item.typeid
						})
						if (currentItem) {
							currentItem.lists.push({
								typeid: data[j].id,
								val: data[j].name
							})
						}
					}
					let filters = self.data.filters
		
					filters = address
					self.setData({
						filters
					})
				}
			}
		})


	},
	getMayjorList(param) {  // 获取专业列表
		const { perPage, mayjorPage, mayjor } = self.data;
		return mayjorList({
			data: { per_page: 100, ...param },
			success(res) {
				let totalPages = res.data.total_pages
				if (mayjorPage <= totalPages) {
					self.setData({
						mayjorPage: mayjorPage + 1,
						mayjor: [...mayjor, ...res.data.items]
					})
					setTimeout(function () {
						self.getMayjorList({ page: mayjorPage + 1 })
					}, 0)
				} else {
					let data = self.data.mayjor;
					let matjor = [];
					for (let i = 0; i < data.length; i++) {
						if (data[i].pid === '3') {
							matjor.push({
								typeid: data[i].id,
								val: data[i].name,
								pid: data[i].pid,
								lists: []
							})
						}
					}
					for (let j = 0; j < data.length; j++) {
						let currentItem = matjor.find(item => {
							return data[j].pid === item.typeid
						})
						if (currentItem) {
							currentItem.lists.push({
								typeid: data[j].id,
								val: data[j].name,
								pid: data[j].pid,
								lists: []
							})
						}
					}
					matjor.map(item => {
						let arr = item.lists
						for (let n = 0; n < data.length; n++) {
							let currentItem = arr.find(item => {
								return data[n].pid === item.typeid
							})
							if (currentItem) {
								currentItem.lists.push({
									typeid: data[n].id,
									val: data[n].name,
									pid: data[n].pid,
									lists: []
								})
							}
						}
					})
					let filters = self.data.filters	
					
					filters= matjor
					self.setData({
						filters
					})
				}
			}
		})
	},
  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function (e) {
		self.data.currentFilter || slef.setData({ currentFilter: [0] })
	},
	onCurrent(e) {
		wx.showLoading({
			title: '加载中...',
		})
		setTimeout(function () {
			wx.hideLoading()
		}, 1000)
		let category = [];
		for (let k in e.detail) {
			category.push(e.detail[k])
		}
		self.setData({
			currentFilter: category
		})
	},
	onShareAppMessage: function (res) {
		return {
			title: '在职研究生网' + self.data.title,
			path: '/pages/classify/classify'
		}
	},
	onTabCnt(e){
		wx.pageScrollTo({
			scrollTop: 0,
			duration: 300
		})
	}
})